<template>
	<!-- 添加小区 -->
	<view class="building-add">
		<view class="add-head">
			<view class="head-left">
				<view class="head-left-1">上传图片</view>
				<view class="head-left-2">图片建议大小不超过2M</view>
			</view>
			<view class="head-right" @click="chooseImage">
				<image class="img" v-if="avator==''" src="../../../../static/icon/jia-.png"></image>
				<image class="img2" v-else :src="avator"></image>
			</view>
		</view>
		<view class="add-content">
			<view class="add-item">
				<view class="item-left">网格<text>*</text></view>
				<view class="item-right" @click="handleSelect">{{ grid }}</view>
			</view>
			<view class="add-item">
				<view class="item-left">名称<text>*</text></view>
				<view class="item-right"><input v-model="name" placeholder-style="color:#999;font-size:26rpx" placeholder="请输入学校名称" /></view>
			</view>
			<view class="add-item">
				<view class="item-left">负责人<text>*</text></view>
				<navigator class="item-right" url="../../Public/People" hover-class="none">{{ resName }}</navigator>
			</view>
			<view class="add-item" @click="gradeShow=true">
				<view class="item-left">小区类型<text>*</text></view>
				<view class="item-right">{{ quartersType_text }}</view>
			</view>
			<navigator class="add-item" url="../../Public/Company" hover-class="none">
				<view class="item-left">物业公司<text>*</text></view>
				<view class="item-right">{{ companyId_text }}</view>
			</navigator>
			<view class="add-item">
				<view class="item-left">占地面积<text>*</text></view>
				<view class="item-right"><input v-model="measure" placeholder-style="color:#999;font-size:26rpx" placeholder="请输入占地面积" /><text>平方米</text></view>
			</view>
			<view class="add-item">
				<view class="item-left">小区地址<text>*</text></view>
				<view class="item-right" @click="chooseAddress">{{ address }}</view>
			</view>
			<view class="add-item">
				<view class="item-left">简介</view>
				<view class="item-right"><textarea v-model="synopsis" placeholder-style="color:#999;font-size:26rpx" placeholder="请输入简介"></textarea></view>
			</view>
		</view>
		<view class="btn">
			<view class="btn-1" @click="handleSubmit">保存</view>
		</view>
		
		<u-toast ref="uToast" />
		<!-- 小区类型 -->
		<u-select v-model="gradeShow" :list="dictList" @confirm="confirmGradeShow"></u-select>
		
		<!-- 列选择器 -->
		<u-popup v-model="selectShow" mode="bottom">
			<view class="gridcontent">
				<view class="item">
					<text @click="TCShow=true">通川区</text>
					<view class="item1" v-for="(item,index) in selectList" :key="index" v-if="TCShow">
						<view class="item2" @click="confirmGird(item.depart.departName,item.depart.id)">
							<text @click="TCShow2=true">{{ item.depart.departName }}</text>
							<view class="item3" v-for="(it,i) in item.son" :key="i" v-if="TCShow2" @click="confirmGird(it.depart.departName,it.depart.id)">
								<text @click="TCShow3=true">{{ it.depart.departName }}</text>
								<view class="item4" v-for="(it2,i2) in it.son" :key="i2" v-if="TCShow3" @click="confirmGird(it2.depart.departName,it2.depart.girdId)">
									{{ it2.depart.departName }}
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default{
		data() {
			return{
				selectShow:false,
				selectList: [],
				TCShow:false,
				TCShow2:false,
				TCShow3:false,
				grid: '塔沱社区',
				gridId:'',
				dictList:[],
				gradeShow:false,
				quartersType:'', // 小区类型
				quartersType_text:'请选择', // 小区类型汉字
				avator:'',// 图片
				address:'请选择', // 地址
				longitude:'',
				latitude:'',
				synopsis:'',// 描述
				resId:'', // 负责人ID
				resName:'请选择', // 负责人姓名
				measure:0,// 面积
				name:'',
				companyId:'',// 物业
				companyId_text:'请选择'
			}
		},
		mounted() {
			this.getGridList()
			this.getDictionaries()
		},
		onShow() {
			if(!uni.getStorageSync('people')) return
			let User = uni.getStorageSync('people')
			// console.log(User)
			this.resName = User.name
			this.resId = User.id
			if(!uni.getStorageSync('company')) return
			let company = uni.getStorageSync('company')
			// console.log(company)
			this.companyId = company.id
			this.companyId_text = company.name
		},
		methods:{
			// 获取网格
			getGridList() {
				this.$u.api.getGridList()
					.then(res => {
						// console.log(res)
						if (res.code == 200) {
							this.selectList = res.result.son
						}
					})
					.catch(err => {
						console.log(err)
						this.$refs.uToast.show({
							title: '获取网格失败'
						})
					})
			},
			confirmGird(data,id) {
				this.grid = data
				this.gridId = id
				// this.selectShow = false
			},
			// 列选择器
			handleSelect() {
				this.selectShow = true
			},
			
			// 小区类型字典
			getDictionaries() {
				let params = {
					dictCode:'quarters_type_dict'
				}
				this.$u.api.dictionaries(params)
					.then(res => {
						// console.log(res)
						if(res.code == 200) {
							res.result.forEach(item => {
								this.dictList.push({
									value:item.value,
									label:item.text
								})
							})
						}
					})
					.catch(err => {
						console.log(err)
						this.$refs.uToast.show({
							title: '获取医院等级失败'
						})
					})
			},
			confirmGradeShow(e) {
				console.log(e)
				this.quartersType = e[0].value
				this.quartersType_text = e[0].label
			},
			
			// 上传图片
			chooseImage() {
				const self = this
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success(res) {
						// console.log(res.tempFilePaths[0])
						uni.uploadFile({
							url: self.$u.http.config.uplodeUrl,
							filePath: res.tempFilePaths[0],
							name: 'file',
							success: (res) => {
								// console.log(JSON.parse(res.data))
								self.avator = JSON.parse(res.data).message
							},
							fail: (res) => {
								console.log(res)
								uni.showToast({
									icon:'none',
									title:'上传失败'
								})
							}
						})
					}
				})
			},
			// 选择地址
			chooseAddress() {
				let self = this
				uni.chooseLocation({
					success(res) {
						// console.log(res)
						self.address = res.address
						self.longitude = res.longitude
						self.latitude = res.latitude
					},
					fail(err) {
						console.log(err)
						uni.showToast({
							icon:'none',
							title:'获取地址失败'
						})
					}
				})
			},
			// 提交
			handleSubmit() {
				if(this.avator == '') {
					return this.$refs.uToast.show({
						title: '请上传图片'
					})
				}
				if(this.gridId == '') {
					return this.$refs.uToast.show({
						title: '请选择网格'
					})
				}
				if(this.name == '') {
					return this.$refs.uToast.show({
						title: '请填写小区名称'
					})
				}
				if(this.resId == '') {
					return this.$refs.uToast.show({
						title: '请选择小区负责人'
					})
				}
				if(this.quartersType == '') {
					return this.$refs.uToast.show({
						title: '请选择小区类型'
					})
				}
				if(this.companyId == '') {
					return this.$refs.uToast.show({
						title: '请选择物业公司'
					})
				}
				if(this.measure == 0) {
					return this.$refs.uToast.show({
						title: '请输入小区占地面积'
					})
				}
				if(this.address == '请选择') {
					return this.$refs.uToast.show({
						title: '请选择小区地址'
					})
				}
				let params = {
					name:this.name,
					img:this.avator,
					girdId:this.gridId,
					resId:this.resId,
					quartersType:this.quartersType,
					companyId:this.companyId,
					measure:this.measure+'平方米',
					address:this.address,
					latitude:this.latitude,
					longitude:this.longitude,
					synopsis:this.synopsis
				}
				console.log(params)
				uni.removeStorageSync('people')
				uni.removeStorageSync('company')
				this.$u.api.addTcQuarters(params)
					.then(res => {
						console.log(res)
						if(res.code == 200) {
							uni.showToast({
								title:'添加小区成功',
								icon:'success'
							})
							this.grid = '塔沱社区'
							this.gridId = ''
							this.quartersType = ''
							this.quartersType_text = '请选择'
							this.avator = ''
							this.address = '请选择'
							this.longitude = ''
							this.latitude = ''
							this.synopsis = ''
							this.resId = ''
							this.resName = '请选择'
							this.measure = 0
							this.name = ''
							this.companyId = ''
							this.companyId_text = '请选择'
						}
					})
					.catch(err => {
						console.log(err)
						uni.showToast({
							title:'添加小区失败',
							icon:'none'
						})
					})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.building-add{
		background-color: #f5f6f7;
		min-height: 100vh;
		.gridcontent{
			height: 40vh;
			width: 100%;
			padding: 20rpx;
			color: #333;
			font-size: 27rpx;
			text-align: left;
			.item{
				width: 100%;
				padding: 5rpx 40rpx;
				overflow-y: auto;
				.item1{
					padding: 5rpx 40rpx;
					.item2{
						padding: 5rpx 40rpx;
						.item3{
							padding: 5rpx 40rpx;
							.item4{
								padding: 5rpx 40rpx;
								
							}
						}
					}
				}
			}
		}
		.add-head{
			display: flex;
			justify-content: space-between;
			align-items: center;
			background-color: #fff;
			height: 180rpx;
			padding: 20rpx;
			.head-left{
				.head-left-1{
					color: #000;
					font-size: 28rpx;
					font-weight: 600;
				}
				.head-left-2{
					color: #999;
					font-size: 24rpx;
					margin-top: 20rpx;
				}
			}
			.head-right{
				background-color: #cecece;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 10rpx;
				width: 150rpx;
				height: 150rpx;
				.img{
					width: 50%;
					height: 50%;
				}
				.img2{
					width: 100%;
					height: 100%;
				}
			}
		}
		.add-content{
			margin-top: 20rpx;
			padding-bottom: 110rpx;
			.add-item{
				display: flex;
				// justify-content: space-between;
				align-items: flex-start;
				padding: 30rpx 20rpx;
				background-color: #fff;
				border-bottom: 1px solid #f4f1f1;
				.item-left{
					color: #333;
					font-size: 28rpx;
					flex: 2;
					text{
						color: #DD524D;
					}
				}
				.item-right{
					font-size: 27rpx;
					color: #999;
					display: flex;
					justify-content: flex-end;
					align-items: center;
					flex: 4;
					input{
						text-align: right;
					}
					input::placeholder{
					    font-size: 26rpx;
					    color: #999;
					}
					textarea{
						
					}
					text{
						display: inline-block;
						margin-left: 5rpx;
					}
				}
			}
		}
		.btn{
			width: 100%;
			height: 110rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #fff;
			font-size: 30rpx;
			background-color: #f4f1f1;
			position: fixed;
			bottom: 0;
			left: 0;
			border-top: 1px solid #CCE6FF;
			.btn-1{
				background-color: #007AFF;
				display: flex;
				justify-content: center;
				align-items: center;
				width: 600rpx;
				height: 80rpx;
				border-radius: 40rpx;
			}
		}
	}
</style>
